package com.majiang.majiang.util;

import java.util.*;

public class Check {
    public static Stack<String> paiStack = new Stack<>();

    public static boolean check(List<Integer> paiList, int pai) {
        List<Integer> tmpList = new ArrayList<>();
        tmpList.addAll(paiList);
        tmpList.add(pai);
        Collections.sort(tmpList, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                if (o1 > o2){
                    return 1;
                } else if (o1 < o2) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        //先查是否是7对 或者 正常叫胡逻辑
        return (tmpList.size() == 13 && checkSevenDouble(tmpList)) || check(tmpList, false);
    }

    //正常胡牌
    private static boolean check(List<Integer> paiList, boolean jiang) {
        boolean ok = false;
        if(paiList.size() <= 0)
            return true;
        List<Integer> tmpList = new ArrayList<>();
        tmpList.addAll(paiList);
        if (!jiang && tmpList.size() >=2 && tmpList.get(0).equals(tmpList.get(1))) {
            Integer pai0 = tmpList.remove(0);
            Integer pai1 = tmpList.remove(0);
            paiStack.push(pai0 + "," + pai1);
            ok = check(tmpList, true);
            if(!ok){
                paiStack.pop();
            }
        }
        tmpList.clear();
        tmpList.addAll(paiList);
        if (!ok && tmpList.size() >= 3) {
            if (tmpList.get(0).equals(tmpList.get(1)) && tmpList.get(1).equals(tmpList.get(2))) {
                Integer pai0 = tmpList.remove(0);
                Integer pai1 = tmpList.remove(0);
                Integer pai2 = tmpList.remove(0);
                paiStack.push(pai0 + "," + pai1 + "," + pai2);
                ok = check(tmpList, jiang);
                if(!ok){
                    paiStack.pop();
                }
            }
            if(!ok){
                tmpList.clear();
                tmpList.addAll(paiList);
                Integer pai0 = tmpList.remove(0);
                for(int i = 0; i < tmpList.size(); i++){
                    if(tmpList.get(i) == pai0+1){
                        Integer pai1 = tmpList.remove(i);
                        for(int j = 0; j < tmpList.size(); j++){
                            if(tmpList.get(j) == pai0+2){
                                Integer pai2 = tmpList.remove(j);
                                paiStack.push(pai0 + "," + pai1 + "," + pai2);
                                ok = check(tmpList, jiang);
                                if(!ok){
                                    paiStack.pop();
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        }
        return ok;
    }

    //七对
    private static boolean checkSevenDouble(List<Integer> paiList) {
        boolean ok = false;
        if(paiList.size() <= 0)
            return true;
        List<Integer> tmpList = new ArrayList<>();
        tmpList.addAll(paiList);
        if (tmpList.size() >=2 && tmpList.get(0).equals(tmpList.get(1))) {
            Integer pai0 = tmpList.remove(0);
            Integer pai1 = tmpList.remove(0);
            paiStack.push(pai0 + "," + pai1);
            ok = checkSevenDouble(tmpList);
            if(!ok){
                paiStack.pop();
            }
        }
        return ok;
    }

    public static void main(String[] args) {
        boolean ok;
        Integer[] c = {11, 12, 13, 14, 15, 16, 17, 18, 19, //1-9万
                21, 22, 23, 24, 25, 26, 27, 28, 29, //1-9饼
                31, 32, 33, 34, 35, 36, 37, 38, 39, //1-9条
                40, 50, 60, 70, 80, 90, 100}; //东南西北中发白
        String s = "";
        List<Integer> pai = new ArrayList<>();
        pai.add(11);
        pai.add(11);
        pai.add(33);
        pai.add(33);
        pai.add(14);
        pai.add(14);
        pai.add(24);
        pai.add(24);
        pai.add(36);
        pai.add(36);
        pai.add(16);
        pai.add(16);
        pai.add(47);
        System.out.print("Case:" + s + "\n");
        ok = false;
        long miSecs = System.currentTimeMillis();
        for (Integer i: c) {
            paiStack.clear();
            if (check(pai, i)) {// 和了
                ok = true;
                int size = paiStack.size();
                while(size-- > 0){
                    String tmpStr = paiStack.pop();
                    System.out.print(tmpStr + " ");
                }
                System.out.println("听牌:" + i);// 那么这副牌听这个牌
            }
        }
        long miSece = System.currentTimeMillis();
        if (!ok)
            System.out.println("还没胡");
        System.out.println("用时:"+(miSece-miSecs)+"ms");
    }

}
